#include "myplayer.h"
#include "ui_myplayer.h"

MyPlayer::MyPlayer(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MyPlayer)
{
    ui->setupUi(this);
    ui->speedSB->setRange(-9,9);

    ui->durationSlider->setRange(0, 10000);
    ui->durationSlider->setSingleStep(1);
}

MyPlayer::~MyPlayer()
{
    delete ui;
}

void MyPlayer::on_updateTime(double currTime, double duration)
{
//    qDebug() <<" 2023.10.13 : "<< currTime << "  " << duration;

    int hours = static_cast<int>(currTime / 3600);
    int minutes = static_cast<int>((currTime - hours * 3600) / 60);
    int seconds = static_cast<int>(currTime - hours * 3600 - minutes * 60);

    QTime qtime(hours, minutes, seconds);
    QString currTimeString = qtime.toString("hh:mm:ss");
    ui->currentTime->setText(currTimeString);

    int hours2 = static_cast<int>(duration / 3600);
    int minutes2 = static_cast<int>((duration - hours2 * 3600) / 60);
    int seconds2 = static_cast<int>(duration - hours2 * 3600 - minutes2 * 60);

    QTime qtime2(hours2, minutes2, seconds2);
    QString durationString = qtime2.toString("hh:mm:ss");
    ui->duration->setText(durationString);

    auto value = static_cast<double>(QTime(0,0).secsTo(qtime))/QTime(0,0).secsTo(qtime2)*10000;
//    qDebug() <<" 2023.10.13 : "<< value;
    ui->durationSlider->setValue(static_cast<int>(value));
}


void MyPlayer::on_startBtn_clicked()
{
    connect(ui->widget->getDemux(), &Demux::sendTime, this, &MyPlayer::on_updateTime);

    QString url = "D:\\MV\\ReadyForIt.mp4";

    ui->widget->setUrl(url);
    ui->widget->paly();
}


void MyPlayer::on_stopBtn_clicked()
{
    ui->widget->stop();
}


void MyPlayer::on_pauseBtn_clicked()
{
    ui->widget->setPause();
}


void MyPlayer::on_speedSB_textChanged(const QString &arg1)
{
    if(arg1.toInt() > 0) {
        ui->widget->setSpeed(1 - arg1.toDouble()/10.0);
    } else if(arg1.toInt() == 0){
        ui->widget->setSpeed(1);
    }else {
        ui->widget->setSpeed(qAbs(arg1.toDouble()));
    }

}


void MyPlayer::on_previousFrameBtn_clicked()
{
    ui->widget->setPreviousFrame();
}




void MyPlayer::on_previousFrameBtn_2_clicked()
{
    ui->widget->seekFrame(false);
}


void MyPlayer::on_nextFrameBtn_clicked()
{
    ui->widget->seekFrame(true);
}

